<template>
    <div class="box">
        <div class="login">
            <h1>登录</h1>
            <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="80px" class="demo-ruleForm">
                <el-form-item label="账号: " prop="username">
                    <el-input v-model.trim="ruleForm.username" autocomplete="off" placeholder="请输入用户账号"></el-input>
                </el-form-item>
                <el-form-item label="密码: " prop="password">
                    <el-input type="password" v-model.trim="ruleForm.password" autocomplete="off"
                        placeholder="请输入登入密码"></el-input>
                </el-form-item>
                <div class="block">
                    <el-slider v-model="value3" :show-tooltip="false"></el-slider>
                </div>
                <el-form-item>
                    <el-button type="primary" style="width: 100%;" @click="login('ruleForm')">登录</el-button>
                </el-form-item>
            </el-form>
            <p>没有账户？<span style="color: skyblue;cursor: pointer;" @click="$router.push({ path: '/register' })">去注册</span>
            </p>
        </div>
    </div>
</template>

<script>
import axios from "@/axios/index";
export default {
    data() {
        return {
            ruleForm: {
                username: '',
                password: '',
                value3: 0,
            },
            rules: {
                username: [
                    {
                        required: true,
                        min: 3,
                        max: 12,
                        message: "账号长度在 6 到 16 个字符",
                        // trigger: "blur",
                    }
                ],
                password: [
                    {
                        required: true,
                        min: 6,
                        max: 16,
                        message: "密码长度在 6 到 16 个字符",
                        trigger: "blur",
                    }
                ],
            }
        };
    },
    methods: {
        login(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    let params = new URLSearchParams();
                    params.append('username', this.ruleForm.username);
                    params.append('password', this.ruleForm.password);
                    this.$axios.post("/api/login", params).then((response) => {
                        console.log(response);
                        //跳转的页面写在此处
                        if (response.data.status !== 0) {
                            return this.$message({
                                type: 'error',
                                message: '登录失败'
                            });
                        }
                        // 将 token 保存到 localStorage
                        let token = response.data.token
                        localStorage.setItem('token', token);
                        this.$message({
                            type: 'success',
                            message: '登录成功'
                        });
                        this.$router.push({ path: '/index' });
                    });
                } else {
                    console.log('error submit!!');
                    return false;
                }
            })
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        }
    }
}
</script>

<style lang="less" scoped>
.box {
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    position: absolute;
    left: 0px;
    top: 0px;
    width: 100%;
    background-color: rgba(236, 239, 244, 1);
    background-image: url(https://www.axureux.com/demo/Templates020/images/%E7%99%BB%E5%BD%95%E7%95%8C%E9%9D%A2_1/u9969_state0.svg);
    background-position: center center;
    background-repeat: no-repeat;

    .login {
        width: max-content;
        height: max-content;
        padding: 20px 40px;
        background: #fff;
        box-shadow: 0px 0px 30px rgba((0), 0, 0, 0.18);

        .el-input {
            width: 300px;
        }
    }
}
</style>